/*
 * main.c
 *
 *  Created on: Aug 3, 2013
 *      Author: lxzhu
 */

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <math.h>
float alpha = 0;
float beta = 0;
float radius=1;

void onInit();
void onDraw();
void onKeyDown(unsigned char key, int x, int y);

void updateLookAt();

int main(int argc, char** argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutCreateWindow("LookAt");
	glutDisplayFunc(onDraw);
	glutKeyboardFunc(onKeyDown);

	glutMainLoop();
	return 0;
}
void onInit(){
	updateLookAt();
}
void onDraw() {
	glClear(GL_COLOR_BUFFER_BIT);
	glutWireTeapot(0.5);
	glutSwapBuffers();
}
void onKeyDown(unsigned char key, int x, int y) {
	switch (key) {
	case 'a':
        alpha-=10;
		break;
	case 'd':
		alpha+=10;
		break;
	case 's':
		beta-=10;
		break;
	case 'w':
		beta+=10;
		break;
	case 'r':
		alpha=0;
		beta=0;

		break;
	default:
		break;
	}
	updateLookAt();
	glutPostRedisplay();
}
void updateLookAt(){
	float ex,ey,ez,ux,uy,uz;
		ex=radius*cos(beta)*cos(alpha);
		ey=radius*cos(beta)*sin(beta);
		ez=radius*sin(alpha);
		ux=0;
		uy=1;
		uz=0;
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		gluLookAt(ex,ey,ez,0,0,0,ux,uy,uz);
}


